package com.ust.app.movie.action.movie;


import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ust.app.movie.constants.Constants;
import com.ust.app.movie.entity.Genre;
import com.ust.app.movie.entity.Movie;
import com.ust.app.movie.service.MovieService;

public class MovieManagerImpl extends ActionSupport implements MovieManager {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		
	private Movie movie; // movie entity
	private List<Genre> genres; // list of genres injected via constructor using spring
	
	private List<MovieDataTable> movieDataTables;
	
	private String action; // button name that determines whether action is delete or edit
	 
	private MovieService movieService; // java bean service injected via constructor using spring
	
	private String genreId;
	
	public MovieManagerImpl(MovieService movieService, List<Genre> genres){
		
		this.movieService = movieService;
		this.genres = genres;
		
	}
	
	public String execute(){
		
		
		return SUCCESS;
	}
	
	public Movie getMovie() {
		
		return this.movie;
	}
	
	public void setMovie(Movie movie) {
		this.movie = movie;
	}

	public List<Genre> getGenres() {
		return genres;
	}
	
	public List<MovieDataTable> getMovieDataTables(){
		return this.movieDataTables;
	}
	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getGenreId() {
		return genreId;
	}

	public void setGenreId(String genreId) {
		this.genreId = genreId;
	}
	
	public String addMovie() {
		try {
			
			movie.setGenre(movieService.getGenreById(Integer.valueOf(getGenreId()).intValue()));
			movieService.addMovie(movie);
			
			setMovie(movieService.getMovie(Integer.valueOf(movie.getId()).intValue()));
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}
	
	public String getAllMoviesByGenre() {
		
		
		setMovie(null);
		
		movieDataTables = new ArrayList<MovieDataTable>();
		
		try {
			List<Movie> movies = this.movieService.getAllMovies();
			
			List<Movie> movieList;
			MovieDataTable movieDataTable;
			
			for(Genre genre : genres){
				movieList = new ArrayList<Movie>();
				movieDataTable = new MovieDataTable();
				
				movieDataTable.setGenre(genre);
				
				for(Movie movie : movies){
					if(movie.getGenre().getId() == genre.getId()){
						movieList.add(movie);
					}
				}
				
				movieDataTable.setMovies(movieList);
				movieDataTables.add(movieDataTable);
			}
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String getMovieById(){
		
		Object movie_id = ActionContext.getContext().getParameters().get("movie_id");
		
		try {
			String[] movie_ids = (String[]) movie_id;
			
			movie = this.movieService.getMovie(Integer.valueOf(movie_ids[0]).intValue());
			
			setGenreId(String.valueOf(movie.getGenre().getId()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String updateMovie(){
		
		try {
			
			movie.setGenre(movieService.getGenreById(Integer.valueOf(getGenreId()).intValue()));
			movieService.updateMovie(movie);
			
			setMovie(movieService.getMovie(movie.getId()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	public String deleteOrEditOrBackToList(){
		
		if(getAction().equals(Constants.DELETE)){
			
			try{
				movieService.deleteMovie(movie);
			}catch(Exception e){
				e.printStackTrace();
			}
			
			return Constants.DELETE_ACTION;
		}
		
		if(getAction().equals(Constants.EDIT)){
			return Constants.EDIT_ACTION;
		}
		
		return Constants.GO_TO_LIST_ACTION;
	}
	
	public String deleteMovieById(){
		
		Object movie_id = ActionContext.getContext().getParameters().get("movie_id");
		String[] movie_ids = (String[]) movie_id;
		
		try {
			movieService.deleteMovieById(Integer.valueOf(movie_ids[0]).intValue());
		} catch (NumberFormatException e) {
			
		} catch (Exception e) {
			
		}
		return SUCCESS;
	}
	
	public String editMovieById(){
		
		Object movie_id = ActionContext.getContext().getParameters().get("movie_id");
		String[] movie_ids = (String[]) movie_id;
		
		try {
			movie = this.movieService.getMovie(Long.valueOf(movie_ids[0]).intValue());
			setGenreId(String.valueOf(movie.getGenre().getId()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
}
